﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.DataAccess;
using Kiiro.ExceptionManagement;
using Kiiro.Validation.Validators.Entities;

namespace Kiiro.Model
{
    public class AttachmentModel
    {
        public static void SaveAttachments ( BaseEntity entity, Guid siteCollectionId, string serverRelativeUrl )
        {
            AttachmentDA.Instance.Save ( entity, siteCollectionId, serverRelativeUrl );
        }

        public static int GetAttachmentCount(string listName, Guid siteCollectionId, string serverRelativeUrl)
        {
            return AttachmentDA.Instance.GetAttachmentCount(listName, siteCollectionId, serverRelativeUrl);
        }

        public static List<Attachment> GetAttachments(string listName, Guid siteCollectionId, string serverRelativeUrl)
        {
            List<Attachment> attachments = AttachmentDA.Instance.GetAttachments(listName, siteCollectionId, serverRelativeUrl);

            if (attachments != null && attachments.Count > 0)
            {
                List<int> ids = attachments.Where ( x => x.CreatedBy != null )
                    .Select<Attachment, int> ( x => x.CreatedBy.Id )
                        .Distinct ( ).ToList ( );

                List<User> users = UserModel.GetUsersByIds ( ids, siteCollectionId, serverRelativeUrl );

                foreach ( Attachment attachment in attachments.Where ( x => x.CreatedBy != null ) )
                    attachment.CreatedBy = users.Find ( x => x.Id == attachment.CreatedBy.Id );
            }

            return attachments;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Model/AttachmentModel.cs $
 * 
 * 5     2/09/10 12:48p Camerons
 * 
 * 4     12/02/09 12:29p Stefanf
 * 
 * 3     17/08/09 10:29a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/